using System;
using Microsoft.SPOT;

namespace System
{
    public class Random
    {
        // Summary:
        //     Initializes a new instance of the System.Random class, using a time-dependent
        //     default seed value.
        public Random()
        {
            Microsoft.SPOT.Math.Randomize();
        }
        //
        // Summary:
        //     Initializes a new instance of the System.Random class, using the specified
        //     seed value.
        //
        // Parameters:
        //   Seed:
        //     A number used to calculate a starting value for the pseudo-random number
        //     sequence. If a negative number is specified, the absolute value of the number
        //     is used.
        //
        // Exceptions:
        //   System.OverflowException:
        //     Seed is System.Int32.MinValue, which causes an overflow when its absolute
        //     value is calculated.
        public Random(int Seed)
        {
            Microsoft.SPOT.Math.Randomize();
        }

        // Summary:
        //     Returns a nonnegative random number.
        //
        // Returns:
        //     A 32-bit signed integer greater than or equal to zero and less than System.Int32.MaxValue.
        public virtual int Next()
        {
            return Microsoft.SPOT.Math.Random(Int32.MaxValue);
        }
        //
        // Summary:
        //     Returns a nonnegative random number less than the specified maximum.
        //
        // Parameters:
        //   maxValue:
        //     The exclusive upper bound of the random number to be generated. maxValue
        //     must be greater than or equal to zero.
        //
        // Returns:
        //     A 32-bit signed integer greater than or equal to zero, and less than maxValue;
        //     that is, the range of return values ordinarily includes zero but not maxValue.
        //     However, if maxValue equals zero, maxValue is returned.
        //
        // Exceptions:
        //   System.ArgumentOutOfRangeException:
        //     maxValue is less than zero.
        public virtual int Next(int maxValue)
        {
            return Microsoft.SPOT.Math.Random(maxValue);
        }
        //
        // Summary:
        //     Returns a random number within a specified range.
        //
        // Parameters:
        //   minValue:
        //     The inclusive lower bound of the random number returned.
        //
        //   maxValue:
        //     The exclusive upper bound of the random number returned. maxValue must be
        //     greater than or equal to minValue.
        //
        // Returns:
        //     A 32-bit signed integer greater than or equal to minValue and less than maxValue;
        //     that is, the range of return values includes minValue but not maxValue. If
        //     minValue equals maxValue, minValue is returned.
        //
        // Exceptions:
        //   System.ArgumentOutOfRangeException:
        //     minValue is greater than maxValue.
        public virtual int Next(int minValue, int maxValue)
        {
            int modulo = maxValue - minValue;
            return Microsoft.SPOT.Math.Random(modulo) + minValue;
        }
        //
        // Summary:
        //     Fills the elements of a specified array of bytes with random numbers.
        //
        // Parameters:
        //   buffer:
        //     An array of bytes to contain random numbers.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        //     buffer is null.
        public virtual void NextBytes(byte[] buffer)
        {
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = (byte)Microsoft.SPOT.Math.Random(byte.MaxValue);
            }
        }
        //
        // Summary:
        //     Returns a random number between 0.0 and 1.0.
        //
        // Returns:
        //     A double-precision floating point number greater than or equal to 0.0, and
        //     less than 1.0.
        public virtual double NextDouble()
        {
            return ((double)Int32.MaxValue) / Microsoft.SPOT.Math.Random(Int32.MaxValue);
        }
        //
        // Summary:
        //     Returns a random number between 0.0 and 1.0.
        //
        // Returns:
        //     A double-precision floating point number greater than or equal to 0.0, and
        //     less than 1.0.
        protected virtual double Sample()
        {
            return NextDouble();
        }
    }
}
